package controllers;

import beans.*;
import models.*;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.With;
import utils.SessionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xieyicheng on 2015/2/4.
 */
@With(value=CheckLogin.class)
public class Members extends Controller {

    public static void home(int page) {
        User user = SessionUtils.getLoginUser();
        String id = SessionUtils.getUserId();
        long total = Article.count("authorId = ? and isDelete != 1 and type !=0", id);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Article> articlelList = Article.find(
                "authorId = ? and isDelete != 1 and type !=0 order by weight , createTime desc",id).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getInstance(1);
        webServiceBean.data = ArticleBean.buildList(articlelList);
        //专辑
        long A_total = Album.count("authorId = ? and isDelete != 1", id);
        page = page <= 0 ? 1 : page;
        PageBean A_pageBean = PageBean.getInstance(page, A_total, 8);
        List<Album> albumList = Album.find(
                "authorId = ? and isDelete != 1 order by createTime desc",id).fetch(
                A_pageBean.getCurPage(), A_pageBean.getPerPage());
        WebServiceBean A_webServiceBean = WebServiceBean.getSuccessInstance();
        A_webServiceBean.data = AlbumBean.bulidList(albumList);
        render(user,webServiceBean,A_webServiceBean,pageBean);

    }
    public static void work(int page) {
        String id = SessionUtils.getUserId();
        long total = Article.count("authorId = ? and isDelete != 1 and type !=0", id);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Article> articlelList = Article.find(
                "authorId = ? and isDelete != 1 and type !=0 order by weight , createTime desc",id).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getInstance(1);
        webServiceBean.data = ArticleBean.buildList(articlelList);
        render(webServiceBean,pageBean);
    }

    public static void album(int page)
    {
        String id = SessionUtils.getUserId();
        long total = Album.count("authorId = ? and isDelete != 1", id);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Album> albumList = Album.find(
                "authorId = ? and isDelete != 1 order by createTime desc",id).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = AlbumBean.bulidList(albumList);
        render(webServiceBean, total, pageBean);
    }

    public static void draft(int page) {
        String userId = SessionUtils.getUserId();
        long total = Article.count("authorId = ? and isDelete != 1 and type = ?",userId,Article.DRAFT);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<Article> articlelList = Article.find(
                "authorId = ? and isDelete != 1 and type = ? order by weight , createTime desc",userId,Article.DRAFT).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getInstance(1);
        webServiceBean.data = ArticleBean.buildList(articlelList);
        render(webServiceBean,pageBean,total);

    }
    public static void changePass() {
        User user = SessionUtils.getLoginUser();
        render(user);    }
    public static void editor() {
        List<TypeName> list = TypeName.findAll();
        User user = SessionUtils.getLoginUser();
        render(user,list);
    }
    public static void sendEmail(int page) {
        User user = SessionUtils.getLoginUser();
        long total = Mail.count("send_id = ? and is_send_delete != 1", user.id);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 7);
        List<Mail> mailList = Mail.find("send_id = ? and is_send_delete != 1 order by createTime desc", user.id)
                .fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = MailBean.bulidList(mailList);
        render(webServiceBean,pageBean,user);
    }
    public static void setPass() {
        render();
    }
    public static void Post(String ArticleId) {
        Article article = null;
        String  url = null;
        if(ArticleId!=null){
             article = Article.findById(ArticleId);
             Photo photo =Photo.findById(article.photoId);
            if(photo!=null){
                url = photo.getPhoto();
            }
        }
        List<TypeName> list = TypeName.findAll();
        render(list,article,url);
    }
    public static void addAlbum(String err) {
        render(err);
    }
    public static void reciveEmail(int page) {
        User user = SessionUtils.getLoginUser();
        user.hasEmail=0;
        user.save();
        long total = Mail.count("receive_id = ? and status = ? and is_receive_delete != 1",user.id,Mail.SENDED);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 7);
        List<Mail> mailList = Mail.find("receive_id = ? and status = ? and is_receive_delete != 1 order by createTime desc", user.id,Mail.SENDED)
                .fetch(pageBean.getCurPage(), pageBean.getPerPage());
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = MailBean.bulidList(mailList);
        render(webServiceBean,pageBean,user);
    }
    public static void albumItem(int page,String albumId) {

        Album album = Album.findById(albumId);
        if(album==null){

        }
        long total = AlbumArticle.count("albumId = ?", albumId);
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 8);
        List<AlbumArticle> aaList = AlbumArticle.find(
                "albumId = ? order by createTime desc",albumId).fetch(
                pageBean.getCurPage(), pageBean.getPerPage());
        List<Article> articleList = new ArrayList<Article>();
        for(int i = 0;i < aaList.size();i++)
        {
            Article article = Article.findById(aaList.get(i).articleId);
            if(article == null)
                continue;
            articleList.add(article);
        }
        User user = User.findById(album.authorId);
        AlbumBean albumBean = AlbumBean.bulid(album);
        WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
        webServiceBean.data = ArticleBean.buildList(articleList);
        render(webServiceBean,pageBean,albumBean,user);
    }

    /**
     *@author 李宇星
     *删除文章接口
     **/
    public static void deleteArticle(String articleId,int type) {
        Article article = Article.getArticleById(articleId);
        if (article != null) {

                Trend.deleteTrend(article.id,Trend.ARTICLE);//先删除动态
                Photo.deletePhoto(article.photoId);//删除封面
                Attachment.deleteAttachment(article.attachmentId);//删除附件
                Collection.deleteRelatedCollection(article.id, Collection.WORK);//删除相关的收藏
                article.delete();
                User user = SessionUtils.getLoginUser();
                user.articleNum--;
                user.save();
        }
        if(type==1){
            Members.draft(0);
        }
        else{
            Members.work(0);
        }
    }
}
